//
//  HBHMainSearchHistoryCell.m
//  HubeiHotel
//
//  Created by 张露杰 on 17/11/16.
//  Copyright © 2017年 张露杰. All rights reserved.
//

#import "HBHMainSearchHistoryCell.h"

typedef void(^HBHMainSearchHistoryBlcok)(NSString *);

@implementation HBHMainSearchHistoryCell
{
    HBHMainSearchHistoryBlcok _complete;
}
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    //    继承父类
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.selectionStyle = 0;
    }
    return self;
}

-(void)loadCellWithComplete:(void(^)(NSString *text))complete
{
    //    重新布局
    for (UIView *view in self.contentView.subviews) {
        [view removeFromSuperview];
    }
    if (complete) {
        _complete = complete;
    }
    NSArray *array = [standfault objectForKey:@"searchHistory"];
    if (array.count == 0) {
        UILabel *label = [TomamaUI createLabel:CGRectMake(0, 0, SCREENW, 60*screenW_6) textAligment:1 font:[UIFont systemFontOfSize:16*screenW_6] textColor:TextBlack text:@"暂无记录"];
        [self.contentView addSubview:label];
    }else{
        UIView *lastView;
        for (int i = 0; i < array.count; i++) {
            UILabel *label;
            if (i == 0) {
                label = [TomamaUI createLabel:CGRectMake(12*screenW_6, 0, [TomamaUI getStringWeithtWith:array[i] andHeigth:32*screenW_6 andFont:[UIFont systemFontOfSize:13*screenW_6]] + 30*screenW_6, 32*screenW_6) textAligment:1 font:[UIFont systemFontOfSize:13*screenW_6] textColor:TextBlack text:array[i]];
            }else{
                label = [TomamaUI createLabel:CGRectMake(CGRectGetMaxX(lastView.frame) + 10*screenW_6, lastView.frame.origin.y, [TomamaUI getStringWeithtWith:array[i] andHeigth:32*screenW_6 andFont:[UIFont systemFontOfSize:13*screenW_6]] + 30*screenW_6, 32*screenW_6) textAligment:1 font:[UIFont systemFontOfSize:13*screenW_6] textColor:TextBlack text:array[i]];
                if (CGRectGetMaxX(label.frame) > SCREENW - 12*screenW_6) {
                    label.frame = CGRectMake(12*screenW_6, CGRectGetMaxY(lastView.frame) + 9*screenW_6, label.frame.size.width, label.frame.size.height);
                }
            }
            label.backgroundColor = [UIColor colorWithHexString:@"#f5f5f5"];
            label.layer.masksToBounds = YES;
            label.layer.cornerRadius = label.frame.size.height/2;
            [self.contentView addSubview:label];
            UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(click:)];
            [label addGestureRecognizer:tap];
            label.userInteractionEnabled = YES;
            lastView = label;
        }
    }
}

-(void)click:(UITapGestureRecognizer *)tap
{
    UILabel *label = (id)tap.view;
    _complete(label.text);
}

@end
